import mss
from PIL import Image

def capture_screen_and_save(output_file):
    with mss.mss() as sct:
        # 获取所有显示器的信息
        monitors = sct.monitors
        # 截取所有显示器的组合图像
        screenshot = sct.grab(monitors[0])  # monitors[0] 通常是主显示器

        # 将截图转换为 PIL 图像
        img = Image.frombytes('RGB', screenshot.size, screenshot.rgb)

        # 保存截图到本地
        img.save(output_file)
        print(f"截图已保存为 {output_file}")

        # 显示截图
        #img.show()

if __name__ == "__main__":
    output_filename = "C:\\Users\\liuxi\\Desktop\\wechat_2025-05-07_110702_312.png"
    capture_screen_and_save(output_filename)